﻿app.controller('PasswordController', function ($scope, $http, $window, $compile, $filter) {
    $scope.IsCheck = false;

    $scope.UserLogin = function (user, psd, type) {
        if (user == null || user == "") {
            alertify.alert("请输入工号");
            return false;
        }
        if (psd == null || psd == "") {
            alertify.alert("请输入密码");
            return false;
        }
        $http({
            method: 'POST',
            url: '../Account/CheckLogin',
            data: { 'userName': user, 'password': psd, 'type': type }
        })
            .success(function (ret) {
                if (ret.Code == 200) {
                    $http({
                        method: 'POST',
                        url: '../Account/CreateMyTask'
                    })
                    window.location = '../Emergency/PlanFormPageList';
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {

            });
    }

    $scope.ShowMyAccount = function () {
        $http({
            method: 'POST',
            url: '../Account/GetUserAccount',
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $scope.MyInfo = ret.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {

            });
    }

    $scope.Test = function () {
        $http.get('http://192.168.0.110:8050/Account/TestOrg').success(function (data) {

            console.log(data);

        });

        //var myUrl = "http://192.168.0.110:8050/Account/TestOrg?callback=JSON_CALLBACK";
        //$http.jsonp(myUrl).success(function (response) {
        //    console.log(response);
        //});
        //$http({
        //    method: 'GET',
        //    url: 'http://192.168.0.110:8050/Account/TestOrg',
        //})
        //    .success(function (ret) {
        //        common.success($scope, null, ret);
        //        if (ret.Code == 100) {
        //            $scope.MyInfo = ret.Data;
        //        }
        //        else {
        //            alertify.alert(ret.Msg);
        //        }
        //    })
        //    .error(function (data, status, headers, config) {

        //    });
    }

    $scope.GetMessagePageList = function (model, page) {
        $scope.DataLoading = true;
        $scope.MyMessageList = [];
        $http({
            method: 'POST',
            url: '../Account/GetHiddenTroublePageList',
            data: { "SearchText": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.MyMessageList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.ShowTip = function (text) {
        alertify.alert(text);
    }

    $scope.checkPassword = function (model) {
        if (model.NewPassword != model.ConfirmPassword) {
            $("#notice").html("两次输入不一致");
        } else {
            $("#notice").html("");
        }
    }

    $scope.CheckNewPassword = function (password) {
        const regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$/;
        if (!regex.test(password)) {
            $scope.ContentCheck = false;
        }
        else {
            $scope.ContentCheck = true;
        }
    }

    $scope.ChangePwd = function (model, valid) {
        $scope.isSubmit = true;
        if (model == null || model.Password == null || model.Password == "") {
            alertify.alert("请输入密码");
            return false;
        }
        if (model.NewPassword == null || model.NewPassword == "") {
            alertify.alert("请输入新密码");
            return false;
        }
        if (model.NewPassword == null || model.NewPassword == "") {
            alertify.alert("请输入新密码");
            return false;
        }
        if ($scope.ContentCheck == false) {
            alertify.alert("密码强度验证未通过");
            return false;
        }
        if (model.NewPassword != model.ConfirmPassword) {
            alertify.alert("两次输入不一致");
            return false;
        } else {
            $("#notice").html("");
        }
        if (valid) {
            $http({
                method: 'POST',
                url: '../Account/ChangePwd',
                data: { 'password': model.Password, 'newpassword': model.NewPassword }
            })
                .success(function (ret) {
                    if (ret.Code == 100) {
                        alertify.alert(ret.Msg);
                        setTimeout(function () {
                            location.href = location.href;
                        }, 3000);
                    } else
                        alertify.alert(ret.Msg);
                })
                .error(function (data, status, headers, config) {
                    common.error($scope);
                })
        }
    }
});


